package tv.huan.web.controller.media;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import tv.huan.common.annotation.Log;
import tv.huan.common.core.controller.BaseController;
import tv.huan.common.core.domain.AjaxResult;
import tv.huan.common.core.page.TableDataInfo;
import tv.huan.common.enums.BusinessType;
import tv.huan.framework.util.ShiroUtils;
import tv.huan.media.domain.Media;
import tv.huan.media.service.IMediaAuditService;
import tv.huan.media.service.IMediaService;
import tv.huan.system.domain.SysUser;

/**
 * 媒资Controller
 * 
 * @author admin
 * @date 2020-04-02
 */
@Controller
@RequestMapping("/media/media")
public class MediaController extends BaseController
{
    @Value("${huan.playClient}")
    private String playClient;

    private String prefix = "media";

    @Autowired
    private IMediaService mediaService;

    @Autowired
    private IMediaAuditService mediaAuditService;

    @RequiresPermissions("media:media:view")
    @GetMapping()
    public String media(ModelMap mmap)
    {
        mmap.put("playClient", playClient);
        return prefix + "/media";
    }

    /**
     * 查询媒资列表
     */
    @RequiresPermissions("media:media:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Media media)
    {
        List<Media> list;
        //当前用户
        SysUser user = ShiroUtils.getSysUser();
        //已经分配小组的审核人员或者非审核人员（管理员）可以查看媒资 0未分配 1已分配
        if (user.getRemark().equals("1") || !user.getRemark().equals("0") ){
            Media selectMedia = mediaService.setMediaTeamId(media,user);
            startPage();
            list = mediaService.selectMediaList(selectMedia);
        }else {
            list = new ArrayList<>();
        }
        return getDataTable(list);
    }

    /**
     * 审核视频
     */
    @RequiresPermissions("media:media:audit")
//    @Log(title = "内容审核", businessType = BusinessType.GRANT)
    @PostMapping("/audit")
    @ResponseBody
    public AjaxResult audit(Integer id, Integer auditState, String auditFailedReason)
    {
        //当前用户
        String user = ShiroUtils.getSysUser().getUserName();
        try {
            return toAjax(mediaAuditService.audit(id,auditState,user,auditFailedReason));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 批量审核
     */
    @RequiresPermissions("media:media:audit")
//    @Log(title = "内容审核", businessType = BusinessType.GRANT)
    @PostMapping( "/auditAll")
    @ResponseBody
    public AjaxResult auditAll(String ids,Integer auditState, String auditFailedReason)
    {
        //当前用户
        String user = ShiroUtils.getSysUser().getUserName();
        String[] idArr = ids.split(",");
        try {
            for (String id : idArr){
                this.mediaAuditService.audit(Integer.parseInt(id),auditState,user,auditFailedReason);
            }
        } catch (Exception e) {
            return error(e.getMessage());
        }
        return success();
    }

    /**
     * 驳回原因
     */
    @RequiresPermissions("media:media:audit")
    @GetMapping("/auditFailed/{ids}")
    public String auditFailed(@PathVariable("ids") String ids, ModelMap mmap)
    {
        mmap.put("ids", ids);
        return prefix + "/auditFailed";
    }

    /**
     * 接收推送数据
     */
//    @Log(title = "接收推送数据", businessType = BusinessType.OTHER)
    @PostMapping( "/initData")
    @ResponseBody
    public AjaxResult initData(@RequestBody JSONObject auditJson) {
        if (null == auditJson){
            return error("数据为空");
        }else {
            try {
                mediaService.initData(auditJson);
            } catch (Exception e) {
                return error(e.getMessage());
            }
            return success("成功推送");
        }
    }


//    /**
//     * 导出媒资列表
//     */
//    @RequiresPermissions("media:media:export")
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(Media media)
//    {
//        List<Media> list = mediaService.selectMediaList(media);
//        ExcelUtil<Media> util = new ExcelUtil<Media>(Media.class);
//        return util.exportExcel(list, "media");
//    }

    /**
     * 新增媒资
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存媒资
     */
    @RequiresPermissions("media:media:add")
    @Log(title = "媒资", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Media media)
    {
        return toAjax(mediaService.insertMedia(media));
    }

    /**
     * 修改媒资
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Media media = mediaService.selectMediaById(id);
        mmap.put("media", media);
        return prefix + "/edit";
    }

    /**
     * 修改保存媒资
     */
    @RequiresPermissions("media:media:edit")
    @Log(title = "媒资", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Media media)
    {
        return toAjax(mediaService.updateMedia(media));
    }

    /**
     * 删除媒资
     */
    @RequiresPermissions("media:media:remove")
    @Log(title = "媒资", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mediaService.deleteMediaByIds(ids));
    }
}
